package ebedynky.services;

import ebedynky.beans.HibernateUtil;
import ebedynky.beans.Offer;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
 * @author davidon1
 */
public class OfferService {

    /**
     * Seesion - current connection to the db.
     */
    private Session session;

    /**
     * Constructor, retrieces the session (connection).
     */
    public OfferService() {
        this.session = HibernateUtil.getSessionFactory().openSession();
    }

    /**
     * Closes the session (connection).
     * @return success/failure.
     */
    public boolean close() {
        this.session.close();
        return !(this.session.isOpen());
    }

    /**
     * Restarts the session (connection).
     * @return success/failure.
     */
    public boolean restart() {
        this.session.close();
        if (this.session.isOpen()) {
            throw new IllegalStateException("Session could not be closed.");
        }
        this.session = HibernateUtil.getSessionFactory().openSession();
        return this.session.isOpen();
    }

    /**
     * Adds offer to the db.
     * @param o Offer to be inserted
     * @return ID of the added offer
     */
    public int addOffer(Offer o) {
        session.beginTransaction();
        int id = getMaxOfferID() + 1;
        o.setOfferID(id);
        id = (Integer) session.save(o);
        session.getTransaction().commit();
        return (id);
    }

    /**
     * Retrieves the maximum id currently in the database of offers.
     * @return Max ID
     */
    public int getMaxOfferID() {
        String hql = "select max(o.offerID) from Offer o";
        List list = session.createQuery(hql).list();
        if (list.get(0) != null) {
            Integer maxoid = (Integer) list.get(0);
            return maxoid;
        } else {
            return 0;
        }
    }

    /**
     * Retrieves a offer identified by the ID.
     * @param id of the Offer
     * @return Offer with the ID given (null if not found)
     */
    public Offer getOfferByID(int id) {
        session.beginTransaction();
        Offer o = (Offer) session.get(Offer.class, new Integer(id));
        session.getTransaction().commit();
        return (o);
    }

    /**
     * Retrieves a list of all offers stored in the db.
     * @return List of all Offers (null if no offers)
     */
    public List<Offer> getAllOffers() {
        session.beginTransaction();
        List<Offer> oList = session.createQuery("from Offer").list();
        session.getTransaction().commit();
        return (oList);
    }

    /**
     * Updates a specified offer identified by ID.
     * @param newOffer an updated offer
     */
    public void updateOffer(Offer newOffer) {
        session.beginTransaction();
        Offer o = (Offer) session.get(Offer.class, new Integer(newOffer.getOfferID()));
        o.setBoxes(newOffer.getBoxes());
        o.setDeliveryPlace(newOffer.getDeliveryPlace());
        o.setNotes(newOffer.getNotes());
        o.setOfferitems(newOffer.getOfferitems());
        o.setSupplier(newOffer.getSupplier());
        o.setSuppliers(newOffer.getSuppliers());
        o.setSubsupply(newOffer.isSubsupply());
        o.setValidFrom(newOffer.getValidFrom());
        o.setValidUntil(newOffer.getValidUntil());
        session.flush();
        session.getTransaction().commit();
        session.clear();
    }

    /**
     * Removes the Offer from db.
     * @param id Offer ID for removal
     * @return true if successful, false otherwise
     */
    public boolean deleteOffer(int id) {
        try {
            session.beginTransaction();
            Offer o = (Offer) session.get(Offer.class, new Integer(id));
            session.delete(o);
            session.getTransaction().commit();
            return (true);
        } catch (HibernateException ex) {
            return (false);
        }
    }
}